package StudyCode.summary.regularexpressionsmatch;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class GreedyAndNongreedyMatchingInARegularExpression {
	public static void main(String[] args) {
		String match = find("A.*c", "AbcAbc"); // AbcAbc
		match = find("A.+", "AbcAbc"); // AbcAbc
		System.out.println("匹配结果: " + match);
		match = find("A.*?c", "AbcAbc"); // Abc
		System.out.println("匹配结果: " + match);
		match = find("A.+?", "AbcAbc"); // Abc
		System.out.println("匹配结果: " + match);
	}

	public static String find(String patternStr, CharSequence input) {
		Pattern pattern = Pattern.compile(patternStr);
		Matcher matcher = pattern.matcher(input);
		if (matcher.find()) {
			return matcher.group();
		}
		return null;
	}
}
